/*
 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
 *
 * This software is open source.
 * See the bottom of this file for the licence.
 */

package org.dom4j.io;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.w3c.dom.DOMImplementation;

/**
 * <p> <code>DOMWriter</code> takes a DOM4J tree and outputs it as a W3C DOM
 * object </p>
 * 
 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
 * @version $Revision: 1.3 $
 */
public class DOMWriter extends AbstractDOMWriter
{   
   public DOMWriter()
   {
      super();
   }

   public DOMWriter(Class domDocumentClass)
   {
      super(domDocumentClass);
   }

   @Override
   protected org.w3c.dom.Document createDomDocument(Document document)
      throws DocumentException
   {
      org.w3c.dom.Document result = null;

      // use the given domDocumentClass (if not null)
      if(domDocumentClass != null)
      {
         try
         {
            result = (org.w3c.dom.Document) domDocumentClass.newInstance();
         }
         catch(Exception e)
         {
            throw new DocumentException("Could not instantiate an instance "
               + "of DOM Document with class: " + domDocumentClass.getName(), e);
         }
      }
      else
      {
         // lets try JAXP first before using the hardcoded default parsers
         result = createDomDocumentViaJAXP();

         if(result == null)
         {
            Class theClass = getDomDocumentClass();

            try
            {
               result = (org.w3c.dom.Document) theClass.newInstance();
            }
            catch(Exception e)
            {
               throw new DocumentException("Could not instantiate an "
                  + "instance of DOM Document " + "with class: "
                  + theClass.getName(), e);
            }
         }
      }

      return result;
   }

   protected org.w3c.dom.Document createDomDocumentViaJAXP()      
   {
      try
      {
         return JAXPHelper.createDocument(false, true);
      }
      catch(Throwable e)
      {
         if(!loggedWarning)
         {
            loggedWarning = true;

            if(SAXHelper.isVerboseErrorReporting())
            {
               // log all exceptions as warnings and carry
               // on as we have a default SAX parser we can use
               System.out.println("Warning: Caught exception attempting "
                  + "to use JAXP to create a W3C DOM " + "document");
               System.out.println("Warning: Exception was: " + e);
               e.printStackTrace();
            }
            else
            {
               System.out.println("Warning: Error occurred using JAXP to "
                  + "create a DOM document.");
            }
         }
      }

      return null;
   }

   @Override
   protected org.w3c.dom.Document createDomDocument(Document document,
      DOMImplementation domImpl) throws DocumentException
   {
      String namespaceURI = null;
      String qualifiedName = null;
      org.w3c.dom.DocumentType docType = null;

      return domImpl.createDocument(namespaceURI, qualifiedName, docType);
   }
}
